#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright @ 2014 OPS, Qunar Inc. (qunar.com)
#
# Author: tingfang.bao <tingfang.bao@qunar.com>
# DateTime: 15-3-11 下午2:38
from flask import Flask, Response

app = Flask(__name__)

csv_data = [["1", "2", "3"], ["4", "5", "6"]]

# 可以实现大文件传送到客户端，服务器端不会占用太多内存
@app.route('/', methods=['GET'])
def generate_large_csv():
    def generate():
        for row in csv_data:
            print row
            yield ','.join(row) + '\n'

    return Response(generate(), mimetype='text/csv', headers={
        "Content-Disposition": 'attachment; filename=\"data.csv\";'
    })


if __name__ == '__main__':
    app.run(debug=True)